package com.hskn.hss.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtil {

    //获取当前时间戳
    public static long formatTimestamp(Date date) {
        if (date == null) {
            return System.currentTimeMillis();
        }
        return date.getTime();
    }

    //获取时间日期格式化
    public static String formatTime2String(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(date);
    }

    public static String formatTime2String(Date date, String str) {
        SimpleDateFormat formatter = new SimpleDateFormat(str);
        return formatter.format(date);
    }

    public static Date formatInt2Date(long timestamp) {
        return new Date(timestamp);
    }

    public static Date formatString2Date(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date formatStringToDate(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date formatDateStringToDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        try {
            return formatter.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    //判断周期性质作业
    public static boolean checkPeriodDate(Date now, Date start, Date end, int step) {
        Date d = start;
        while (end.compareTo(d) > 0) {
            if (now.compareTo(d) == 0) {
                return true;
            }
            d = new Date(d.getTime() + 1000 * 60 * 60 * 24 * step);
        }
        return false;
    }

    public static void main(String[] args) {
        checkPeriodDate(new Date(), new Date(), new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 7), 2);
    }
}
